import { DrawType } from '@/utils/json2image.js';
export const getCustCertJson = (dataInfo, json2image) => {
  let jsonData = [];
  if (!dataInfo) {
    return jsonData;
  }
  let bgItem = {
    type: DrawType.IMAGE,
    url: dataInfo.bgImgUrl,
    left: 0,
    top: 0,
    width: json2image.width,
    height: json2image.height
  };
  jsonData.push(bgItem);
  if (!dataInfo.customizeControl) {
    return jsonData;
  }
  for (let item of dataInfo.customizeControl) {
    if (item.type == 1) {
      const imgUrlMap = {
        1: dataInfo.qrCodeUrl,
        2: dataInfo.homeQrcode
      };
      let imgUrl = imgUrlMap[item.redirectTo] || item.img;
      const qrItem = {
        type: DrawType.IMAGE,
        url: imgUrl,
        left: item.left,
        top: item.top,
        width: item.width,
        height: item.height
      };
      jsonData.push(qrItem);
    } else if (item.type == 2) {
      const textBoxItem = {
        type: DrawType.TEXT_BOX,
        content: item.name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(textBoxItem);
    } else if (item.type == 3) {
      let left = item.left + item.width / 2 - 46;
      const avatarItem = {
        type: DrawType.IMAGE,
        url: dataInfo.headimgurl,
        left: left,
        top: item.top,
        width: 92,
        height: 92,
        radius: '50%'
      };
      jsonData.push(avatarItem);
      let name = dataInfo.nickname;
      const nicknameBoxItem = {
        type: DrawType.TEXT_BOX,
        content: name,
        left: item.left,
        top: item.top + 92,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(nicknameBoxItem);
    } else if (item.type == 6) {
      let left = item.left + item.width / 2 - 46;
      const avatarItem = {
        type: DrawType.IMAGE,
        url: dataInfo.headimgurl,
        left: left,
        top: item.top,
        width: 92,
        height: 92,
        radius: '50%'
      };
      jsonData.push(avatarItem);
    } else if (item.type == 7) {
      let name = item.nameTypeShow == 2 && dataInfo.realName ? dataInfo.realName : dataInfo.nickname;
      const nicknameBoxItem = {
        type: DrawType.TEXT_BOX,
        content: name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(nicknameBoxItem);
    } else if (item.type == 8) {
      const custImgItem = {
        type: DrawType.IMAGE,
        url: item.img,
        left: item.left,
        top: item.top,
        width: item.width,
        height: item.height
      };
      jsonData.push(custImgItem);
    } else if (item.type == 9) {
      const textBoxItem = {
        type: DrawType.TEXT_BOX,
        content: item.name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(textBoxItem);
    } else if (item.type == 10) {
      let name = `${dataInfo.number}`;
      const textBoxItem = {
        type: DrawType.TEXT_BOX,
        content: name,
        left: item.left,
        top: item.top,
        maxWidth: item.width,
        height: item.height,
        fontSize: item.fontSize,
        letterSpacing: item.letterSpacing,
        color: item.color,
        textAlign: item.textAlign,
        textBaseline: item.verticalAlign,
        verticalAlign: item.verticalAlign,
        lineHeight: item.lineHeight,
        fontWeight: item.fontWeight
      };
      jsonData.push(textBoxItem);
    }
  }
  return jsonData;
};
export const getVerCertJson = (dataInfo, json2image) => {
  let jsonData = [];
  if (!dataInfo) {
    return jsonData;
  }
  const width = json2image.width;
  const height = json2image.height;
  let bgItem = {
    type: DrawType.IMAGE,
    url: dataInfo.bgImgUrl,
    left: 0,
    top: 0,
    width: width,
    height: height
  };
  jsonData.push(bgItem);
  let titleTextItem = {
    type: DrawType.TEXT,
    content: dataInfo.title,
    fontSize: 60,
    weight: 600,
    textAlign: 'center',
    color: '#333',
    left: width / 2,
    top: 130
  };
  jsonData.push(titleTextItem);
  if (dataInfo.enableAvatarShow == 1) {
    let avatarItem = {
      type: DrawType.IMAGE,
      url: dataInfo.headimgurl,
      left: width / 2 - 60,
      top: 290,
      width: 120,
      height: 120,
      radius: '50%'
    };
    jsonData.push(avatarItem);
  }
  if (dataInfo.nameTypeShow == 1 || dataInfo.nameTypeShow == 2) {
    let name = dataInfo.nameTypeShow == 2 && dataInfo.realName ? dataInfo.realName : dataInfo.nickname;
    let nameTextItem = {
      type: DrawType.TEXT,
      content: name,
      fontSize: 32,
      textAlign: 'center',
      weight: 600,
      color: '#333',
      left: width / 2,
      top: 430
    };
    jsonData.push(nameTextItem);
  }
  //获取证书文案长度，如果长度在一行内，则居中显示，如果超过则居左,并且头两个字空出
  const LONG_TEXT_MAX_WIDTH = 504;
  const lontContentFontInfo = {
    fontSize: 28,
  }
  let longContentWidth = json2image.getTextWidth(dataInfo.longContent, lontContentFontInfo);
  let textAlign = longContentWidth <= LONG_TEXT_MAX_WIDTH ? 'center' : 'left';
  let longTextleft = longContentWidth <= LONG_TEXT_MAX_WIDTH ? width / 2 : (width - LONG_TEXT_MAX_WIDTH) / 2;
  let textIndent = longContentWidth <= LONG_TEXT_MAX_WIDTH ? 0 : 28 * 2;
  let longContentTextItem = {
    ...lontContentFontInfo,
    type: DrawType.TEXT,
    content: dataInfo.longContent,
    textAlign: textAlign,
    textIndent: textIndent,
    color: '#333',
    left: longTextleft,
    top: 515,
    maxWidth: LONG_TEXT_MAX_WIDTH,
  };
  jsonData.push(longContentTextItem);
  //获取二维码底下文字高度,计算出二维码的top位置，盒子高度为210，二维码和底部文字需要在盒子内居中显示，则top = 盒子top +  (盒子高度 - 二维码高度 - 文字高度) / 2
  const BOTTOM_BOX_HEIGHT = 210; //底部盒子高度
  const BOX_TOP = 1030; //底部盒子Y坐标初始值
  const BOX_LEFT = 65; //底部盒子X坐标初始值
  const QR_TEXT_MAX_WIDTH = 180; //二维码底部文字最大宽度
  const QR_BOX_WIDTH = 310; //二维码盒子宽度
  const CERT_INFO_BOX_WIDTH = 310; //右侧证书信息宽度
  const CERT_TEXT_INFO_MAX_WIDTH = 255; //右侧证书文字最大宽度


  let qrTextOptions = {
      content: dataInfo.qrCodeText,
      fontSize: 20,
      lineHeight: 24,
      maxWidth: QR_TEXT_MAX_WIDTH,
  }
  let qrTextHeight = json2image.getTextHeight(qrTextOptions);
  let qrCodeTop = BOX_TOP + (BOTTOM_BOX_HEIGHT - 140 - qrTextHeight) / 2;

  //显示二维码或者证书信息，如果只有一个显示则居中
  let isShowQr = dataInfo.enableShowQrCode == 1;
  let isShowCertInfo = dataInfo.enableInstitution == 1 || dataInfo.enableGrantTime == 1 || dataInfo.enableNumber == 1 || dataInfo.enableStamp != 0;
  if (isShowQr) {
    const imgUrlMap = {
      1: dataInfo.qrCodeUrl,
      2: dataInfo.homeQrcode,
      3: dataInfo.customizeQrCode
    };
    //如果两个都显示，则居左，如果显示一个则居中
    let left = isShowCertInfo ? BOX_LEFT + (QR_BOX_WIDTH - 140 ) / 2 : width / 2 - 70;
    let imgUrl = imgUrlMap[dataInfo.redirectTo];
    let qrItem = {
      type: DrawType.IMAGE,
      url: imgUrl,
      left: left,
      top: qrCodeTop,
      width: 140,
      height: 140
    };
    let qrTextLeft = isShowCertInfo ? BOX_LEFT + QR_BOX_WIDTH / 2 : width / 2;
    let qrTextItem = {
      type: DrawType.TEXT,
      content: dataInfo.qrCodeText,
      fontSize: 18,
      lineHeight: 24,
      textAlign: 'center',
      color: '#999',
      left: qrTextLeft,
      maxWidth: QR_TEXT_MAX_WIDTH,
      top: qrCodeTop + 140 + 9
    };
    jsonData.push(qrItem);
    jsonData.push(qrTextItem);
  }
  //显示徽章图标
  if (dataInfo.enableStamp != 0) {
    //徽章图标X坐标
    // 显示二维码的话 -- 左侧距离 = 左侧间距宽度 + 二维码盒子宽度 （右侧盒子宽度 - 徽章盒子宽度） / 2
    // 隐藏二维码的话 --左侧距离 = 总宽度/2 - 徽章盒子宽度/2
    let stampLeft = isShowQr ? BOX_LEFT + QR_BOX_WIDTH + (CERT_INFO_BOX_WIDTH - 120 ) / 2 : width / 2 - 60;
    //徽章图标Y坐标为盒子的中间 = 底部位置 + （盒子高度-图标高度）/2
    let stampTop = BOX_TOP + (BOTTOM_BOX_HEIGHT - 120) / 2;
    let stampItem = {
      type: DrawType.IMAGE,
      url: dataInfo.stampImg,
      left: stampLeft,
      top: stampTop,
      width: 120,
      height: 120
    };
    jsonData.push(stampItem);
  }
  //获取底部右边证书信息文字总高度
  let certInfoTextHeight = 0;
  //发证机构高度
  if (dataInfo.enableInstitution == 1) {
    let institutionOptions = {
      content: dataInfo.institutionName,
      fontSize: 24,
      lineHeight: 34,
      maxWidth:CERT_TEXT_INFO_MAX_WIDTH,
    }
    let institutionTextHeight = json2image.getTextHeight(institutionOptions)
    certInfoTextHeight += institutionTextHeight;
  }
  //发证日期高度
  if (dataInfo.enableGrantTime == 1) {
    let timeOptions = {
      content: dataInfo.createdAt,
      fontSize: 18,
      lineHeight: 24,
      maxWidth:CERT_TEXT_INFO_MAX_WIDTH,
    }
    let timeTextHeight = json2image.getTextHeight(timeOptions)
    certInfoTextHeight = certInfoTextHeight + timeTextHeight + 14;
  }
  //发证
  if (dataInfo.enableNumber == 1) {
    let numberOptions = {
      content: dataInfo.number,
      fontSize: 18,
      lineHeight: 24,
      maxWidth:CERT_TEXT_INFO_MAX_WIDTH,
    }
    let numberTextHeight = json2image.getTextHeight(numberOptions)
    certInfoTextHeight = certInfoTextHeight + numberTextHeight + 8;
  }
  //根据文字总高度，文字需要居中显示，计算出第一行文字的Y坐标值
  let certInfoTextTop = BOX_TOP + (BOTTOM_BOX_HEIGHT - certInfoTextHeight) / 2;
  let certInfoBoxLeft = isShowQr ? BOX_LEFT + QR_BOX_WIDTH + (CERT_INFO_BOX_WIDTH - CERT_TEXT_INFO_MAX_WIDTH) / 2 : width / 2 - CERT_TEXT_INFO_MAX_WIDTH / 2;
  if (dataInfo.enableInstitution == 1) {
    let institutionNameItem = {
      type: DrawType.TEXT,
      content: dataInfo.institutionName,
      fontSize: 24,
      lineHeight: 34,
      weight: 600,
      maxWidth: 255,
      color: '#333',
      left: certInfoBoxLeft,
      top: certInfoTextTop
    };
    jsonData.push(institutionNameItem);
    let institutionTextHeight = json2image.getTextHeight(institutionNameItem);
    certInfoTextTop += institutionTextHeight;
  }
  if (dataInfo.enableGrantTime == 1) {
    certInfoTextTop += 14;
    let timeItem = {
      type: DrawType.TEXT,
      content: dataInfo.createdAt,
      fontSize: 18,
      lineHeight: 24,
      maxWidth: 255,
      color: '#999',
      left: certInfoBoxLeft,
      top: certInfoTextTop
    };
    jsonData.push(timeItem);
    certInfoTextTop += 24;
  }
  if (dataInfo.enableNumber == 1 && dataInfo.number) {
    certInfoTextTop += 8;
    let numberItem = {
      type: DrawType.TEXT,
      content: dataInfo.number,
      fontSize: 18,
      lineHeight: 24,
      maxWidth: 255,
      color: '#999',
      left: certInfoBoxLeft,
      top: certInfoTextTop
    };
    jsonData.push(numberItem);
  }

  return jsonData;
};
export const getHorCertJson = (dataInfo, json2image) => {
  let jsonData = [];
  if (!dataInfo) {
    return jsonData;
  }
  const width = json2image.width;
  const height = json2image.height;
  let bgItem = {
    type: DrawType.IMAGE,
    url: dataInfo.bgImgUrl,
    left: 0,
    top: 0,
    width: width,
    height: height
  };
  jsonData.push(bgItem);
  let titleTextItem = {
    type: DrawType.TEXT,
    content: dataInfo.title,
    fontSize: 20,
    weight: 600,
    textAlign: 'center',
    color: '#333',
    left: width / 2,
    top: 67
  };
  jsonData.push(titleTextItem);
  if (dataInfo.enableAvatarShow == 1) {
    let avatarItem = {
      type: DrawType.IMAGE,
      url: dataInfo.headimgurl,
      left: width / 2 - 72 / 2,
      top: 154,
      width: 72,
      height: 72,
      radius: '50%'
    };
    jsonData.push(avatarItem);
  }
  if (dataInfo.nameTypeShow == 1 || dataInfo.nameTypeShow == 2) {
    let name = dataInfo.nameTypeShow == 2 && dataInfo.realName ? dataInfo.realName : dataInfo.nickname;
    let nameTextItem = {
      type: DrawType.TEXT,
      content: name,
      fontSize: 16,
      textAlign: 'center',
      weight: 600,
      color: '#333',
      left: width / 2,
      top: 232
    };
    jsonData.push(nameTextItem);
  }
  //获取证书文案长度，如果长度在一行内，则居中显示，如果超过则居左，并且头两个字空出
  const LONG_TEXT_MAX_WIDTH = 560;
  const lontContentFontInfo = {
    fontSize: 18,
  }
  let longContentWidth = json2image.getTextWidth(dataInfo.longContent, lontContentFontInfo);
  let textAlign = longContentWidth <= LONG_TEXT_MAX_WIDTH ? 'center' : 'left';
  let left = longContentWidth <= LONG_TEXT_MAX_WIDTH ? width / 2 : (width - LONG_TEXT_MAX_WIDTH) / 2;
  let textIndent = longContentWidth <= LONG_TEXT_MAX_WIDTH ? 0 : 28 * 2;
  let longContentTextItem = {
    ...lontContentFontInfo,
    type: DrawType.TEXT,
    content: dataInfo.longContent,
    textAlign: textAlign,
    textIndent: textIndent,
    color: '#333',
    left: left,
    top: 295,
    maxWidth: LONG_TEXT_MAX_WIDTH,
  };
  jsonData.push(longContentTextItem);
  const BOTTOM_BOX_HEIGHT = 150; //底部盒子高度
  const QR_BOX_WIDTH = 495; //左侧二维码盒子宽度
  const CERT_INFO_BOX_WIDTH = 495; //右侧证书信息盒子宽度
  const BOTTOM_BOX_LEFT = (width - QR_BOX_WIDTH - CERT_INFO_BOX_WIDTH) / 2;
  const BOTTOM_BOX_TOP = 565;

  let isShowQr = dataInfo.enableShowQrCode == 1;
  if (isShowQr) {
    const QR_CODE_PADDING_LEFT = 40;
    //二维码Y坐标 = 底部高度 + （盒子高度-二维码高度）/2
    let qrCodeTop = BOTTOM_BOX_TOP + (BOTTOM_BOX_HEIGHT - 108) / 2;
    let qrCodeLeft = BOTTOM_BOX_LEFT + QR_CODE_PADDING_LEFT;
    const imgUrlMap = {
      1: dataInfo.qrCodeUrl,
      2: dataInfo.homeQrcode,
      3: dataInfo.customizeQrCode
    };
    let imgUrl = imgUrlMap[dataInfo.redirectTo];
    let qrItem = {
      type: DrawType.IMAGE,
      url: imgUrl,
      left: qrCodeLeft,
      top: qrCodeTop,
      width: 108,
      height: 108
    };
    let qrTextLeft = qrCodeLeft + 108 + 12;
    let qrTextOptions = {
    type: DrawType.TEXT,
    content: dataInfo.qrCodeText,
    fontSize: 20,
    lineHeight: 28,
    color: '#999',
    left: qrTextLeft,
    maxWidth: 200,
    };
    let qrTextHeight = json2image.getTextHeight(qrTextOptions);
    let qrTextTop = BOTTOM_BOX_TOP + (BOTTOM_BOX_HEIGHT - qrTextHeight) / 2;
    let qrTextItem = {
      ...qrTextOptions,
      top: qrTextTop
    };
    jsonData.push(qrItem);
    jsonData.push(qrTextItem);
   }
    //显示徽章图标
  if (dataInfo.enableStamp != 0) {
    //左侧距离 = 左侧间距宽度75 + (600 - 二维码盒子宽度76 + 二维码文字宽度170) + （右侧盒子宽度300 - 徽章盒子宽度120） / 2
    let stampLeft = width / 2 + CERT_INFO_BOX_WIDTH / 2 ;
    let stampTop = BOTTOM_BOX_TOP + (BOTTOM_BOX_HEIGHT - 136) / 2;
    let stampItem = {
      type: DrawType.IMAGE,
      url: dataInfo.stampImg,
      left: stampLeft,
      top: stampTop,
      width: 136,
      height: 136
    };
    jsonData.push(stampItem);
  }
   //获取底部右边证书信息文字总高度
   let certInfoTextHeight = 0;
   //发证机构高度
   if (dataInfo.enableInstitution == 1) {
     let institutionOptions = {
       content: dataInfo.institutionName,
       fontSize: 22,
       lineHeight: 30,
       maxWidth:CERT_INFO_BOX_WIDTH,
     }
     let institutionTextHeight = json2image.getTextHeight(institutionOptions)
     certInfoTextHeight += institutionTextHeight;
   }
   //发证日期高度
   if (dataInfo.enableGrantTime == 1) {
     let timeOptions = {
       content: dataInfo.createdAt,
       fontSize: 20,
       lineHeight: 28,
       maxWidth:CERT_INFO_BOX_WIDTH,
     }
     let timeTextHeight = json2image.getTextHeight(timeOptions)
     certInfoTextHeight = certInfoTextHeight + timeTextHeight + 8;
   }
   //发证
   if (dataInfo.enableNumber == 1) {
     let numberOptions = {
       content: dataInfo.number,
       fontSize: 20,
       lineHeight: 28,
       maxWidth:CERT_INFO_BOX_WIDTH,
     }
     let numberTextHeight = json2image.getTextHeight(numberOptions)
     certInfoTextHeight = certInfoTextHeight + numberTextHeight + 10;
   }
   //根据文字总高度，文字需要居中显示，计算出第一行文字的Y坐标值
   let certInfoTextTop = BOTTOM_BOX_TOP + (BOTTOM_BOX_HEIGHT - certInfoTextHeight) / 2;
  //证书信息靠右对齐，间距30
  const certInfoTextLeft = width / 2 + CERT_INFO_BOX_WIDTH - 30;
  if (dataInfo.enableInstitution == 1) {
    let institutionNameItem = {
      type: DrawType.TEXT,
      content: dataInfo.institutionName,
      fontSize: 22,
      weight: 600,
      lineHeight: 30,
      textAlign:'right',
      color: '#333',
      left: certInfoTextLeft,
      top: certInfoTextTop
    };
    jsonData.push(institutionNameItem);
    certInfoTextTop += 30;
  }
  if (dataInfo.enableGrantTime == 1) {
    certInfoTextTop += 8;
    let timeItem = {
      type: DrawType.TEXT,
      content: dataInfo.createdAt,
      fontSize: 20,
      lineHeight: 28,
      textAlign:'right',
      color: '#999',
      left: certInfoTextLeft,
      top: certInfoTextTop
    };
    jsonData.push(timeItem);
    certInfoTextTop += 20;
  }
  if (dataInfo.enableNumber == 1 && dataInfo.number) {
    certInfoTextTop += 10;
    let numberItem = {
      type: DrawType.TEXT,
      content: dataInfo.number,
      fontSize: 20,
      lineHeight: 28,
      textAlign:'right',
      color: '#999',
      left: certInfoTextLeft,
      top: certInfoTextTop
    };
    jsonData.push(numberItem);
  }
  return jsonData;
};
